/**
 * %datax%
 * %v1.0%
 */
package com.alibaba.datax.elasticresource.watcher;

import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import com.alibaba.datax.common.util.Configuration;
import com.alibaba.datax.core.util.container.CoreConstant;
import com.leehom.arch.base.redis.client.RedisClient;
import com.leehom.arch.datax.report.repository.elasticsearch.config.ElasticsearchBeanConfig;
import com.leehom.arch.datax.report.repository.redis.config.RedisClientConfig;
import com.leehom.arch.onesearch.index.DocumentService;

import redis.clients.jedis.JedisPoolConfig;

/**
 * @类名: ReportBeanConfig
 * @说明: 报告bean配置
 *
 * @author   leehom
 * @Date	 2022年11月1日 上午9:58:08
 * 修改记录：
 *
 * @see 	 
 */
public class WatcherReporterBeanConfig {

	// 构建elasticsearch文档服务
	public static DocumentService buildElasticsearchClient(Configuration conf) {
		Configuration local = conf.getConfiguration(CoreConstant.DATAX_CORE_STATISTICS_SUMMARY_REPORT_ELASTICSEARCH);
		// 构建document service
		String scheamConfPath = WatcherConfigKey.elasticsaerchSchemaConfPath(local);
		String nodes = WatcherConfigKey.elasticsaerchNodes(local);
		return ElasticsearchBeanConfig.documentService(nodes, scheamConfPath);
		
	}
	
	// 单机
	public static RedisClient standaloneRedis(Configuration conf) {
		Configuration local = conf.getConfiguration(CoreConstant.DATAX_CORE_STATISTICS_SNAPSHOT_REPORT_REDIS);
		//
		String nodes = WatcherConfigKey.redisClientNodes(local);
		int db = WatcherConfigKey.redisClientDatabase(local);
		String password = WatcherConfigKey.redisClientPassword(local);
		//
		RedisStandaloneConfiguration standalone = RedisClientConfig.redisStandaloneConfiguration(nodes, db, password);
		JedisConnectionFactory cf = RedisClientConfig.jedisConnectionFactory(standalone);
		return RedisClientConfig.redisClient(cf);

	}
	
	// 哨兵
	public static RedisClient sentinelRedis(Configuration conf) {
		// 构建jedis pool
		JedisPoolConfig jedisPoolConfig  = RedisClientConfig.jedisPoolConfig();
		//
		Configuration local = conf.getConfiguration(CoreConstant.DATAX_CORE_STATISTICS_SUMMARY_REPORT_ELASTICSEARCH);
		String nodes = WatcherConfigKey.redisClientNodes(local);
		int db = WatcherConfigKey.redisClientDatabase(local);
		String master = WatcherConfigKey.redisClientMaster(local);
		String password = WatcherConfigKey.redisClientPassword(local);
		//
		RedisSentinelConfiguration sentinel = RedisClientConfig.redisSentinelConfiguration(nodes, db, master, password);
		JedisConnectionFactory cf = RedisClientConfig.jedisSentinelConnectionFactory(sentinel, jedisPoolConfig);
		return RedisClientConfig.redisClient(cf);

	}
	
	// 集群
	public static RedisClient clusterRedis(Configuration conf) {
		// 构建jedis pool
		JedisPoolConfig jedisPoolConfig  = RedisClientConfig.jedisPoolConfig();
		//
		Configuration local = conf.getConfiguration(CoreConstant.DATAX_CORE_STATISTICS_SUMMARY_REPORT_ELASTICSEARCH);
		String nodes = WatcherConfigKey.redisClientNodes(local);
		int db = WatcherConfigKey.redisClientDatabase(local);
		String password = WatcherConfigKey.redisClientPassword(local);
		//
		RedisClusterConfiguration cluster = RedisClientConfig.redisClusterConfiguration(nodes, db, password);
		JedisConnectionFactory cf = RedisClientConfig.jedisClusterConnectionFactory(cluster, jedisPoolConfig);
		return RedisClientConfig.redisClient(cf);

	}
	
}
